package com.seeyon.apps.u8cloud.event;

import com.seeyon.apps.collaboration.manager.ColManager;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.apps.u8cloud.manager.U8CloudApiManager;
import com.seeyon.apps.u8cloud.manager.U8CloudVoucherManager;
import com.seeyon.apps.u8cloud.manager.VoucherRecordManager;
import com.seeyon.apps.u8cloud.model.ResultData;
import com.seeyon.apps.u8cloud.model.VoucherParams;
import com.seeyon.apps.u8cloud.model.VoucherRecord;
import com.seeyon.apps.u8cloud.model.VoucherResult;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.DateUtil;
import com.seeyon.ctp.util.annotation.Inject;
import com.seeyon.ctp.util.concurrent.ExecutorServiceFactory;
import com.seeyon.ctp.util.json.JSONUtil;
import com.seeyon.ctp.workflow.event.AbstractWorkflowEvent;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;
import com.seeyon.ctp.workflow.util.WorkflowEventConstants;
import org.apache.commons.logging.Log;

import java.util.List;

/**
 * @ClassName : TravelVoucherEvent
 * @Description :差旅凭证生成事件
 * @CreateDate : 2025/8/19 10:45
 * @Author : CheungHowrAn
 * @Version : 1.0.0
 * @PackageName com.seeyon.apps.u8cloud.event
 * @Project : BSiEE本涩_A8_V9.0SP1
 */
public class TravelVoucherEvent extends AbstractWorkflowEvent {
    private static Log log = CtpLogFactory.getLog(TravelVoucherEvent.class);
    private static String serviceUrl = AppContext.getSystemProperty("u8cloud.serviceUrl");

    private ColManager colManager;
    
    public ColManager getColManager() {
		return colManager;
	}

	public void setColManager(ColManager colManager) {
		this.colManager = colManager;
	}
	
    @Override
    public String getId() {
        return "travelVoucherEvent";
    }

    @Override
    public String getLabel() {
        return "差旅凭证生成";
    }

    @Override
    public ApplicationCategoryEnum getAppName() {
        return ApplicationCategoryEnum.form;
    }

    @Override
    public WorkflowEventConstants.WorkflowEventType getType() {
        return WorkflowEventConstants.WorkflowEventType.Ext;
    }

    @Inject
    private U8CloudApiManager u8CloudApiManager;
    @Inject
    private U8CloudVoucherManager u8CloudVoucherManager;
    @Inject
    private VoucherRecordManager voucherRecordManager;

    /**
     * @MethodName: onBeforeFinishWorkitem
     * @Description: 节点完成前事件
     * @CreateDate: 2025/8/19 13:47
     * @Author: CheungHowrAn
     * @Param data
     * @Return WorkflowEventResult
     */
    public synchronized WorkflowEventResult onBeforeFinishWorkitem(WorkflowEventData data) {
        WorkflowEventResult workflowEventResult = new WorkflowEventResult();
//        try {
//            log.info("============差旅凭证生成事件===============");
//            FormDataMasterBean master = (FormDataMasterBean) data.getBusinessData().get("formDataBean");
//            FormBean formBean = (FormBean) data.getBusinessData().get("formBean");
//            //     VoucherParams voucherParams = u8CloudVoucherManager.getTransferVoucherParams(master, formBean);
//            VoucherParams voucherParams = u8CloudVoucherManager.getTransferVoucherParams(master,formBean);
//            VoucherResult voucherResult = u8CloudApiManager.createVoucher(voucherParams);
//            if (voucherResult.getStatus().equals("success")) {
//                List<ResultData> resultData = voucherResult.getResultData();
//                //todo
//                String pkVoucher = resultData.get(0).getNo();
//                master.addFieldValue(formBean.getFieldBeanByDisplay("回传凭证").getName(), pkVoucher);
//            } else {
//                workflowEventResult.setAlertMessage("付款凭证生成失败:" + voucherResult.getErrormsg());
//            }
//            
//            ColSummary summary = null;
//    		summary = colManager.getColSummaryById(data.getSummaryId());
//            VoucherRecord voucherRecord = new VoucherRecord();
//            voucherRecord.setDataId(master.getId().toString());
//            voucherRecord.setFormId(formBean.getId().toString());
//            voucherRecord.setSubject(summary.getSubject());
//            if (voucherResult.getStatus().equals("success")) {
//                voucherRecord.setVoucherId(voucherResult.getResultData().get(0).getPk_voucher());
//                voucherRecord.setVoucherCode(voucherResult.getResultData().get(0).getNo());
//            }
//            voucherRecord.setStatus(voucherResult.getStatus());
//            voucherRecord.setServiceUrl(serviceUrl + "/u8cloud/api/gl/voucher/insert");
//            voucherRecord.setParams(JSONUtil.toJSONString(voucherParams));
//            voucherRecord.setResult(JSONUtil.toJSONString(voucherResult));
//            voucherRecord.setCreateDate(DateUtil.get19DateAndTime());
//            ExecutorServiceFactory.getDefaultThreadPool().submit(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        voucherRecordManager.saveDockingRecords(voucherRecord);
//                    } catch (Exception e) {
//                        log.error("保存对接日志异常：", e);
//                    }
//                }
//            });
//        } catch (Exception e) {
//            log.error("差旅凭证生成事件异常", e);
//        }

        return workflowEventResult;
    }

}
